package com.calvin.study.Listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;

import com.calvin.study.service.OnlineNumberCounter;

import cn.hutool.core.date.DateUtil;

@Component
public class WebSocketDisconnectListener implements ApplicationListener<SessionDisconnectEvent> {
	@Autowired
	private OnlineNumberCounter counter;

	@Override
	public void onApplicationEvent(SessionDisconnectEvent event) {
		StompHeaderAccessor accessor = StompHeaderAccessor.wrap(event.getMessage());
		String sessionId = accessor.getSessionId();
		System.out.println(DateUtil.now() + "，sessionId：" + sessionId + "已断开");
		// 在线人数递减
		counter.decrement();
	}
}
